import { BlockEnum } from '@/components/workflow/types'
import { HttpRequestNodeType } from './types'
import { ALL_COMPLETION_AVAILABLE_BLOCKS } from '../../constants'

export const getHttpRequestDefaultParams = () => {
  return {
    method: 'GET',
    url: '',
    headers: [
      {
        key: '',
        value: ''
      }
    ],
    params: [
      {
        key: '',
        value: ''
      }
    ],
    body: {
      type: 'none',
      content: null
    },
    connectTimeout: 30000,
    readTimeout: 30000,
    failureRetry: false, // 失败重试
    retryTimes: 3,
    retryInterval: 3000, // 3秒
    inputList: [
      {
        variable: 'input',
        value: undefined,
        type: 'cite',
        value_selector: []
      }
    ],
    outputList: [
      {
        variable: 'response',
        value: '响应结果',
        type: 'string'
      },
      {
        variable: 'status_code',
        value: '响应状态码',
        type: 'int'
      },
    ]
  }
}

export default {
  defaultValue: { ...getHttpRequestDefaultParams() },
  getAvailablePrevNodes() {
    const nodes = ALL_COMPLETION_AVAILABLE_BLOCKS.filter(type => type !== BlockEnum.End)
    return nodes
  },
  getAvailableNextNodes() {
    const nodes = ALL_COMPLETION_AVAILABLE_BLOCKS
    return nodes
  },
  checkValid(payload: HttpRequestNodeType) {
    let isValid = true
    let errorMessages = ''
    if (!payload.url) {
      isValid = false
      errorMessages = '请求地址不能为空'
    }
    return {
      isValid,
      errorMessage: errorMessages
    }
  }
}